<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-form class="mb-[-16px]" :model="queryParams" inline>
                <el-form-item label="授权推广账号ID" prop="account_id">
                    <el-input class="w-[280px]" v-model="queryParams.account_id" clearable placeholder="请输入授权推广账号ID" />
                </el-form-item>
                <el-form-item label="广告主ID" prop="ad_account_id">
                    <el-input class="w-[280px]" v-model="queryParams.ad_account_id" clearable placeholder="请输入广告主ID" />
                </el-form-item>
                <el-form-item label="广告主名称" prop="corporation_name">
                    <el-input class="w-[280px]" v-model="queryParams.corporation_name" clearable
                        placeholder="请输入广告主名称" />
                </el-form-item>
                <el-form-item label="账号创建时间" prop="create_time">
                    <daterange-picker v-model:startTime="queryParams.start_time"
                        v-model:endTime="queryParams.end_time" />
                </el-form-item>


                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none" v-loading="pager.loading" shadow="never">
            <div class="flex items-center">
                <el-button v-perms="['ads.tencent_advertising_user_ad/delete']" :disabled="!selectData.length"
                    @click="handleDelete(selectData)">
                    删除
                </el-button>

                <!-- 配置数量 -->
                <div class="config-stats-container">
                    <div class="config-stat-item">
                        <div class="config-stat-content">
                            <div class="config-stat-label">授权主体</div>
                            <div class="config-stat-value">
                                <span class="current-count">{{ vdata.zt_num }}</span>
                                <span class="separator">/</span>
                                <span class="total-count">{{ vdata.company_num === 0 ? '无限制' : vdata.company_num
                                    }}</span>
                            </div>
                        </div>
                    </div>

                    <!-- <div class="config-stat-item">
                    <div class="config-stat-content">
                        <div class="config-stat-label">可授权主体数</div>
                        <div class="config-stat-value">
                            <span class="current-count">0</span>
                            <span class="separator">/</span>
                            <span class="total-count">100</span>
                        </div>
                    </div>
                </div> -->

                    <div class="config-stat-item">
                        <div class="config-stat-content">
                            <div class="config-stat-label">账户数</div>
                            <div class="config-stat-value">
                                <span class="current-count">{{ vdata.account_num }}</span>
                                <span class="separator">/</span>
                                <span class="total-count">{{ vdata.tencent_advertising_num === 0 ? '无限制' :
                                    vdata.tencent_advertising_num }}</span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="mt-4">
                <el-table :data="pager.lists" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55" />
                    <el-table-column label="授权推广账号ID" prop="account_id" show-overflow-tooltip />
                    <el-table-column label="广告主ID" prop="ad_account_id" show-overflow-tooltip />
                    <el-table-column label="广告主名称" prop="corporation_name" show-overflow-tooltip />

                    <el-table-column label="操作" width="120" fixed="right">
                        <template #default="{ row }">
                            <el-button v-perms="['ads.tencent_advertising_user_ad/delete']" type="danger" link
                                @click="handleDelete(row.id)">
                                删除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
    </div>
</template>

<script lang="ts" setup name="tencentAdvertisingUserAdLists">
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import { apiTencentAdvertisingUserAdLists, apiTencentAdvertisingUserAdDelete, getCount } from '@/api/tencent_advertising_user_ad'
import { timeFormat } from '@/utils/util'
import feedback from '@/utils/feedback'
import { Check, Plus } from '@element-plus/icons-vue'

// 是否显示编辑框
const showEdit = ref(false)

// 查询条件
const queryParams = reactive({
    tenant_id: '',
    account_id: '',
    ad_account_id: '',
    corporation_name: '',
    create_time: '',
    start_time: '',
    end_time: '',
})

//限制数量
const vdata = reactive({
    account_num: 0,
    company_num: 0,
    tencent_advertising_num: 0,
    zt_num: 0,
})


// 选中数据
const selectData = ref<any[]>([])

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
    selectData.value = val.map(({ id }) => id)
}

// 获取字典数据
const { dictData } = useDictData('')

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: apiTencentAdvertisingUserAdLists,
    params: queryParams
})

// 添加
const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

// 编辑
const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.setFormData(data)
}

// 删除
const handleDelete = async (id: number | any[]) => {
    await feedback.confirm('确定要删除？')
    await apiTencentAdvertisingUserAdDelete({ id })
    getLists()
}

getLists()

const getCountData = async () => {
    const res = await getCount()
    vdata.account_num = res.account_num
    vdata.company_num = res.company_num
    vdata.tencent_advertising_num = res.tencent_advertising_num
    vdata.zt_num = res.zt_num
}

getCountData()
</script>

<style scoped>
/* 配置统计样式 */
.config-stats-container {
    display: flex;
    gap: 12px;
    margin-left: 16px;
    align-items: center;
}

.config-stat-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 12px;
    background: #f8fafc;
    border: 1px solid #e5e7eb;
    border-radius: 6px;
    transition: all 0.3s ease;
    min-width: 140px;
    height: 32px;
}

.config-stat-item:hover {
    background: #f1f5f9;
    border-color: #3b82f6;
}

.config-stat-icon {
    width: 40px;
    height: 40px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    color: white;
    background: linear-gradient(135deg, #10b981, #34d399);
    box-shadow: 0 3px 10px rgba(16, 185, 129, 0.3);
    flex-shrink: 0;
}

.config-stat-icon.available {
    background: linear-gradient(135deg, #3b82f6, #60a5fa);
    box-shadow: 0 3px 10px rgba(59, 130, 246, 0.3);
}

.config-stat-content {
    flex: 1;
    min-width: 0;
    display: flex;
    align-items: center;
    gap: 8px;
}

.config-stat-label {
    font-size: 12px;
    color: #6b7280;
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.config-stat-value {
    display: flex;
    align-items: center;
    gap: 2px;
    font-size: 14px;
    font-weight: 600;
    color: #1f2937;
}

.current-count {
    color: #10b981;
}

.separator {
    color: #9ca3af;
    font-weight: 500;
}

.total-count {
    color: #6b7280;
    font-weight: 600;
}

/* 响应式设计 */
@media (max-width: 768px) {
    .config-stats-container {
        flex-direction: column;
        gap: 12px;
        margin-left: 0;
        margin-top: 12px;
    }

    .config-stat-item {
        min-width: auto;
        width: 100%;
    }
}
</style>
